package com.b1502.store2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.b1502.store2.R;
import com.b1502.store2.adapter.ConfirmAdapter;
import com.b1502.store2.model.OrderForm;
import com.b1502.store2.network.PostParams;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.b1502.store2.widget.MyListView;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.List;

/**
 * 订单确认页面
 */
public class ConfirmOrderActivity extends BaseActivity {
    private TextView shouhuo, t, spPrice, yfAdd, yhFavorable, zjPrice;
    private MyListView confirm_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);
        initStoreBar("确定订单");
        storeBar.hideBackButton();
        intiView();
        //进行取值
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        getOrder("orderId", id);
       /* getAccount();
        confirmOrder("706651fc-7d52-406e-a752-0d4f2b6f06c1","","");
        String[] productIds = {"157fbb6d-750d-41ec-b8da-047a7f96ca4b"};
        removeItems(productIds);*/
    }

    /**
     * 获取id
     */
    private void intiView() {
        shouhuo = (TextView) findViewById(R.id.shouhuo);
        //运费
        t = (TextView) findViewById(R.id.t);
        //商品钱数
        spPrice = (TextView) findViewById(R.id.sp);
        //额外添加钱数
        yfAdd = (TextView) findViewById(R.id.yf);
        //优惠钱数
        yhFavorable = (TextView) findViewById(R.id.yh);
        //总钱数
        zjPrice = (TextView) findViewById(R.id.zj);
        //集合
        confirm_list = (MyListView) findViewById(R.id.confirm_list);

    }

    /**
     * 获取订单
     */
    public void getOrder(String param, String orderId) {
        StoreParams params = new StoreParams(UrlUtil.GetOrder, param, orderId);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //进行解析
                Log.i("TAG", "onSuccess: ========" + result);
                Gson gson = new Gson();
                OrderForm orderForm = gson.fromJson(result, OrderForm.class);
                setText(orderForm);
                setList(orderForm);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    /**
     * 集合适配
     */
    private void setList(OrderForm orderForm){
        if(orderForm != null){
            List<OrderForm.OrderDetailsBeanHeadline> orderDetails =
                    orderForm.getOrderDetails();
            //进行适配
            ConfirmAdapter confirmAdapter = new ConfirmAdapter(this,orderDetails);
            confirm_list.setAdapter(confirmAdapter);
        }
    }
    /**
     *  赋值
     */
    private void setText(OrderForm orderForm){
        shouhuo.setText(orderForm.getReceiptAddress());
        t.setText(orderForm.getFreight()+"");
        spPrice.setText(orderForm.getAmount()+"");
        yfAdd.setText(orderForm.getFreight()+"");
        yhFavorable.setText(orderForm.getDiscount());
        zjPrice.setText(orderForm.getSum()+"");
    }

    /**
     * 获取账户余额
     */
    public void getAccount() {
        StoreParams params = new StoreParams(UrlUtil.GetAccount);
        x.http().get(params, new Callback.CommonCallback<String>() {

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 确认订单
     *
     * @param orderId 订单ID
     * @param remark
     * @param invoice
     */
    public void confirmOrder(String orderId, String remark, String invoice) {
        StoreParams params = new StoreParams(UrlUtil.ConfirmOrder);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(orderId, remark, invoice);
        params.setBodyContent(new Gson().toJson(postParams));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                //TODO 支付页面
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 移除购物车
     *
     * @param productIds 产品ID集合
     */
    public void removeItems(String[] productIds) {
        StoreParams params = new StoreParams(UrlUtil.RemoveItems);
        params.setAsJsonContent(true);
        PostParams postParams = new PostParams(productIds);
        params.setBodyContent(new Gson().toJson(postParams));

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onSuccess(String result) {
                // TODO 更新购物车数量
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}